define([
	'selectModify',
	'layer',
	'../../Shop/module/STH_main.js',
	'checkAll',
	'iCheck',
	'imgCenter'
], function(selectModify, layer, main) {
	//活动
	return {

		//商品图片居中函数
		serviceItemImgCenter: function(thisPanel, thisEdit, key) {
			$(thisPanel).find(".serviceItem-list.style-small .serviceItem-img, .serviceItem-list.style-big .serviceItem-img").each(function() {
				$(this).height($(this).parents("li").width());
			});
			$(thisPanel).find(".serviceItem-list.style-list .serviceItem-img").each(function() {
				$(this).height(80);
			});
			$(thisPanel).find('.serviceItem-img').imgCenter();
		},

		//加入模块
		serviceItemAddM: function(con) {

			var _this = this,
				key = con.key,
				prop = con.prop,
				order = con.order, 
				cate_name = prop.cate_name,
				size = prop.size,
				cate_id = prop.cate_id,
				newPanel = main.getPanel("serviceItem"),
				newEdit = main.getEdit("serviceItem");
			//抢购
			if(prop.cate_id == 1) { 
				ItemList = con.AtList;
			}
			//团购
			else if(prop.cate_id == 2) {
				ItemList = con.AgList;
			} else {
				ItemList = con.AguList; 
			}
			//右侧edit属性
			$(newEdit).attr("key", key);
			$(newEdit).find("input[name='size'][value='" + size + "']").attr("checked", 'checked');
			$(newEdit).find("select[name='choiceitem']").val(cate_id);

			$(newEdit).css("display", "none");

			//左侧panel属性
			$(newPanel).attr("order", order);
			$(newPanel).attr("key", key);
			$(newPanel).find(".serviceItem-list").attr("class", "serviceItem-list style-" + size);

			//插入panel(延时顺序重定位)
			var edLength = $(".hb_box .panel-comp").length;
			if($(".hb_box .panel-comp").length < 1 || order > parseInt($(".hb_box .panel-comp").eq(edLength - 1).attr("order"))) {
				$(".hb_box").append(newPanel);
			} else if(order < parseInt($(".hb_box .panel-comp").eq(0).attr("order"))) {
				$(".hb_box").prepend(newPanel);
			} else {
				for(var r = 0; r < edLength; r++) {
					if(order > parseInt($(".hb_box .panel-comp").eq(r).attr("order")) && order < parseInt($(".hb_box .panel-comp").eq(r + 1).attr("order"))) {
						$(".hb_box .panel-comp").eq(r).after(newPanel);
						break;
					}
				}
			}

			//插入edit
			$(".edit-panel").append(newEdit);

			//活动循环再拉取
			var newPanelLi = $(newPanel).find(".moban").html(),
				newEditLi = $(newEdit).find(".moban").html();

			$(newEdit).find(".serviceItem-list-show ul").html("");
			$(newPanel).find(".serviceItem-list ul").html("");
			$(newEdit).find("select").val(prop.cate_id);
			
			$(newPanel).find(".serviceItem-list").attr("class", "serviceItem-list style-" + prop.size);
			g_data[key].ItemList=[];
			for(var d = 0; d < ItemList.length; d++) {
				var row = ItemList[d];
				var newobj = {
								"Fid": row.Fid,
								"Fimg": row.Fimg,
								"Ftotal": row.Ftotal,
								"Fsalenum": row.Fsalenum,
								"Foprice": row.Foprice,
								"Fsaleprice": row.Fsaleprice,
								"Flimitnum": row.Flimitnum,
								"Fname": row.Fname,
								'Fsummary': row.Fsummary
						},
					nPLi = main.formatTemplate(newobj, newPanelLi),
					nELi = main.formatTemplate(newobj, newEditLi); 
				$(newPanel).find(".serviceItem-list ul").append(nPLi);
				$(newEdit).find(".serviceItem-list-show ul").append(nELi);
 				
				g_data[key].ItemList.push({
							"Ftype":prop.cate_id,
							"Ftypeid": row.Fid
						});

				//图片居中
				_this.serviceItemImgCenter(newPanel, newEdit, key);

			
		}

		//调用本模块的操作  
		_this.serviceItemActive(newPanel, newEdit, key);

	},

	//得到当前分组的index
	getGroupIndex: function(obj, id) {
			var index = 0;
			for(var i = 0; i < obj.length; i++) {
				if(id == obj[i].group_id) {
					index = i;
					break;
				}
			}
			return index;
		},

		//常规操作
		serviceItemActive: function(thisPanel, thisEdit, key) {
			var _this = this;

			/*icheck初始化*/
			$(thisEdit).find("input").iCheck(main.iCheckOption);
			g_data[key].prop.size="small";
			/*select初始化 以及选择分组操作*/
			$(thisEdit).find("select").selectModify({
				onSelectChange: function(e, attrs) {
					if($(e).attr("name") == "choiceitem") {
						var cate_id = attrs.value,
							cate_name = attrs.cate_name;
						if(cate_id == "" || cate_id == g_data[key].prop.cate_id) return;
						//改变活动显示方式
						var newPanelLi = $(main.getPanel("serviceItem")).find(".serviceItem-list ul").html(),
							newEditLi = $(main.getEdit("serviceItem")).find(".serviceItem-list-show ul").html();
						$(thisPanel).find(".serviceItem-list ul").html(newPanelLi);
						$(thisEdit).find(".serviceItem-list-show ul").html(newEditLi);
						g_data[key].ItemList = [];
						g_data[key].prop.cate_id = cate_id;
						g_data[key].prop.cate_name = cate_name;
					}
				}
			});

			//选择活动
			$(thisEdit).find(".choiceServiceItem").unbind("click").click(function() {
				//此处应发AJAX请求，暂用模似
				g_data[key].prop.cate_id = $(thisEdit).find("select").val();

				var cateId = g_data[key].prop.cate_id;

				if(cateId == "1") { //闪购活动
					main.commonDialog3('vstore/tb_activity_time/Sql.html?ischeck=1', '闪购活动', 800, 600, function(newArray) {

						var listmbE = $(thisEdit).find(".moban").html(),
							listmbP = $(thisPanel).find(".moban").html();
						for(var g = 0; g < newArray.length; g++) {
							var row = newArray[g];
							var newobj = {
								"Fid": row.Fid,
								"Fimg": row.Fimg,
								"Ftotal": row.Ftotal,
								"Fsalenum": row.Fsalenum,
								"Foprice": row.Foprice,
								"Fsaleprice": row.Fsaleprice,
								"Flimitnum": row.Flimitnum,
								"Fname": row.Fname,
								'Fsummary': row.Fsummary
							};
							//加入编辑模板
							if($(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").length > 0) {

								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".com-list-box-choice-pic").html("<img src='" + newobj.Fimg + "'>");
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".c333").html(row.Fsummary);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemPricePrevious .mt10").html(newobj.Foprice);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemPriceNow .mt10").html(newobj.Fsaleprice);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemSales .mt10").html(newobj.Fsalenum);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).attr("rel", "");
							} else {
								var newStrE = main.formatTemplate(newobj, listmbE);
								$(thisEdit).find(".serviceItem-list-show ul").append(newStrE);
							}
							//加入显示模板
							if($(thisPanel).find(".serviceItem-list ul li[rel='yuan']").length > 0) {
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find("img").attr("src", newobj.Fimg);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-title").html(row.Fsummary);

								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-price").html("￥" + newobj.Fsaleprice);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find("del").html("￥" + newobj.Foprice);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-price").attr("yuandata", newobj.Fsaleprice);

								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-sale").html("已售：" + newobj.Fsalenum);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).attr("rel", "");
							} else {
								var newStrP = main.formatTemplate(newobj, listmbP);
								console.log(newStrP);
								$(thisPanel).find(".serviceItem-list ul").append(newStrP);
							}

							//图片居中
							g_data[key].ItemList.push({
								"Ftype": 4,
								"Ftypeid": row.Fid
							});
							_this.serviceItemImgCenter(thisPanel, thisEdit, key);

						}
						$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").remove();
						$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").remove();

					});

				} else if(cateId == "2") { //团购活动
					main.commonDialog3('vstore/tb_activity_group/Sql.html?ischeck=1', '团购活动', 800, 600, function(newArray) {
						var listmbE = $(thisEdit).find(".moban").html(),
							listmbP = $(thisPanel).find(".moban").html();
						for(var g = 0; g < newArray.length; g++) {
							var row = newArray[g];
							var newobj = {
								"Fid": row.Fid,
								"Fimg": row.Fimg,
								"Ftotal": row.Ftotal,
								"Fsalenum": row.Fsalenum,
								"Foprice": row.Foprice,
								"Fsaleprice": row.Fsaleprice,
								"Flimitnum": row.Flimitnum,
								"Fname": row.Fname,
								'Fsummary': row.Fsummary
							};
							//加入编辑模板
							if($(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").length > 0) {

								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".com-list-box-choice-pic").html("<img src='" + newobj.Fimg + "'>");
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".c333").html(row.Fsummary);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemPricePrevious .mt10").html(newobj.Foprice);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemPriceNow .mt10").html(newobj.Fsaleprice);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemSales .mt10").html(newobj.Fsalenum);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).attr("rel", "");
							} else {
								var newStrE = main.formatTemplate(newobj, listmbE);
								$(thisEdit).find(".serviceItem-list-show ul").append(newStrE);
							}
							//加入显示模板
							if($(thisPanel).find(".serviceItem-list ul li[rel='yuan']").length > 0) {
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find("img").attr("src", newobj.Fimg);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-title").html(row.Fsummary);

								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-price").html("￥" + newobj.Fsaleprice);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find("del").html("￥" + newobj.Foprice);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-price").attr("yuandata", newobj.Fsaleprice);

								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-sale").html("已售：" + newobj.Fsalenum);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).attr("rel", "");
							} else {
								var newStrP = main.formatTemplate(newobj, listmbP);
								$(thisPanel).find(".serviceItem-list ul").append(newStrP);
							}

							//图片居中 
							g_data[key].ItemList.push({
								"Ftype": 5,
								"Ftypeid": row.Fid
							});
							_this.serviceItemImgCenter(thisPanel, thisEdit, key);
						}
						$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").remove();
						$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").remove();

					});

				} else if(cateId == "3") { //拼团活动
					main.commonDialog3('vstore/tb_activity_group_user/Sql.html?ischeck=1', '拼团活动', 800, 600, function(newArray) {

						var listmbE = $(thisEdit).find(".moban").html(),
							listmbP = $(thisPanel).find(".moban").html();
						for(var g = 0; g < newArray.length; g++) {
							var row = newArray[g];
							var newobj = {
								"Fid": row.Fid,
								"Fimg": row.Fimg,
								"Ftotal": row.Ftotal,
								"Fsalenum": row.Fsalenum,
								"Foprice": row.Foprice,
								"Fsaleprice": row.Fsaleprice,
								"Flimitnum": row.Flimitnum,
								"Fname": row.Fname,
								'Fsummary': row.Fsummary
							};

							//加入编辑模板
							if($(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").length > 0) {

								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".com-list-box-choice-pic").html("<img src='" + newobj.Fimg + "'>");
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".c333").html(row.Fsummary);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemPricePrevious .mt10").html(newobj.Foprice);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemPriceNow .mt10").html(newobj.Fsaleprice);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).find(".serviceItemSales .mt10").html(newobj.Fsalenum);
								$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").eq(0).attr("rel", "");
							} else {
								var newStrE = main.formatTemplate(newobj, listmbE);
								$(thisEdit).find(".serviceItem-list-show ul").append(newStrE);
							}
							//加入显示模板
							if($(thisPanel).find(".serviceItem-list ul li[rel='yuan']").length > 0) {
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find("img").attr("src", newobj.Fimg);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-title").html(row.Fsummary);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-price").html("￥" + newobj.Fsaleprice);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find("del").html("￥" + newobj.Foprice);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-price").attr("yuandata", newobj.Fsaleprice);

								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).find(".serviceItem-sale").html("已售：" + newobj.Fsalenum);
								$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").eq(0).attr("rel", "");
							} else {
								var newStrP = main.formatTemplate(newobj, listmbP);
								$(thisPanel).find(".serviceItem-list ul").append(newStrP);
							}

							//图片居中
							g_data[key].ItemList.push({
								"Ftype": 6,
								"Ftypeid": row.Fid
							});
							_this.serviceItemImgCenter(thisPanel, thisEdit, key);

						}
						$(thisEdit).find(".serviceItem-list-show ul li[rel='yuan']").remove();
						$(thisPanel).find(".serviceItem-list ul li[rel='yuan']").remove();

					});

				}

			});

			//参数调整函数
			function propChange(thisPanel, thisEdit, key) {

				//显示风格
				$(thisEdit).find("input[name='size']").on('ifChecked', function() {
					var sizeVal = $(this).val();
					$(thisPanel).find(".serviceItem-list").attr("class", "serviceItem-list style-" + sizeVal);
					g_data[key].prop["size"] = sizeVal;
					_this.serviceItemImgCenter(thisPanel, thisEdit, key);
				});
			}

			//活动删除,上下移功能
			function imgDelete(thisPanel, thisEdit, key) {
				$(thisEdit).undelegate(".com-list-box-delete i", 'click').delegate(".com-list-box-delete i", 'click', function() {
					var shunxu = $(thisEdit).find(".com-list-box-delete i").index(this),
						style_num = g_data[key].prop.size;
					if($(thisPanel).find(".serviceItem-list ul li").length <= 1) {
						$(thisPanel).find(".serviceItem-list ul li:eq(0)").attr("rel", "yuan");
						$(thisPanel).find(".serviceItem-list ul li:eq(0)").find("a").attr("href", "");
						$(thisPanel).find(".serviceItem-list ul li:eq(0)").find("img").attr("src", app.baseUrl + "/seller/images/shop_decorate/serviceItem_default_img.png");
						$(thisPanel).find(".serviceItem-list ul li:eq(0)").find(".serviceItem-title").html("第1个活动");
						$(thisPanel).find(".serviceItem-list ul li:eq(0)").find(".serviceItem-price").attr("yuandata", "￥价格");
						$(thisPanel).find(".serviceItem-list ul li:eq(0)").find(".serviceItem-price").html("￥价格");
						$(thisPanel).find(".serviceItem-list ul li:eq(0)").find("del").html("￥市场价");
						$(thisPanel).find(".serviceItem-list ul li:eq(0)").find(".serviceItem-sale").html("已售:数量");

						//图片居中
						_this.serviceItemImgCenter(thisPanel, thisEdit, key);
					} else {
						$(thisPanel).find(".serviceItem-list ul li:eq('" + shunxu + "')").remove();
					}

					if($(thisEdit).find(".serviceItem-list-show ul li").length <= 1) {
						$(thisEdit).find(".serviceItem-list-show ul li:eq(0)").attr("rel", "yuan");
						$(thisEdit).find(".serviceItem-list-show ul li:eq(0)").find(".com-list-box-choice-pic").html("<div class='default-text'><span>商品</span><span>图片</span></div>");
						$(thisEdit).find(".serviceItem-list-show ul li:eq(0)").find(".c333").html("第1个活动");
						$(thisEdit).find(".serviceItem-list-show ul li:eq(0)").find(".serviceItemPricePrevious .mt10").html("0.0");
						$(thisEdit).find(".serviceItem-list-show ul li:eq(0)").find(".serviceItemPriceNow .mt10").html("0.0");
						$(thisEdit).find(".serviceItem-list-show ul li:eq(0)").find(".serviceItemSales .mt10").html("0.0");
					} else {
						$(this).parents("li").remove();
					}
					g_data[key].ItemList.splice(shunxu, 1);
				});

				//上移一个位置				
				$(thisEdit).undelegate(".com-list-box-operate .arrow-up", 'click').delegate(".com-list-box-operate .arrow-up", 'click', function() {
					if($(this).parents("li").find(".com-list-box-choice-pic img").length < 1) return;
					var index = $(thisEdit).find(".com-list-box-operate .arrow-up").index(this),
						htmlE = $(this).parents("li").prop("outerHTML"),
						htmlP = $(thisPanel).find(".serviceItem-list ul li:eq(" + index + ")").prop("outerHTML");
					if(index > 0) {
						$(thisEdit).find(".serviceItem-list-show ul li:eq(" + (index - 1) + ")").before(htmlE);
						$(thisEdit).find(".serviceItem-list-show ul li:eq(" + (index + 1) + ")").remove();
						$(thisPanel).find(".serviceItem-list ul li:eq(" + (index - 1) + ")").before(htmlP);
						$(thisPanel).find(".serviceItem-list ul li:eq(" + (index + 1) + ")").remove();
						main.exchange(g_data[key].ItemList, (index - 1), index);

					}
				});

				//下移一个位置
				$(thisEdit).undelegate(".com-list-box-operate .arrow-down", 'click').delegate(".com-list-box-operate .arrow-down", 'click', function() {
					if($(this).parents("li").find(".com-list-box-choice-pic img").length < 1) return;
					var index = $(thisEdit).find(".com-list-box-operate .arrow-down").index(this),
						lilen = $(thisEdit).find(".com-list-box-operate .arrow-down").length,
						htmlE = $(this).parents("li").prop("outerHTML"),
						htmlP = $(thisPanel).find(".serviceItem-list ul li:eq(" + index + ")").prop("outerHTML");
					if(index < (lilen - 1)) {
						$(thisEdit).find(".serviceItem-list-show ul li:eq(" + (index + 1) + ")").after(htmlE);
						$(thisEdit).find(".serviceItem-list-show ul li:eq(" + index + ")").remove();
						$(thisPanel).find(".serviceItem-list ul li:eq(" + (index + 1) + ")").after(htmlP);
						$(thisPanel).find(".serviceItem-list ul li:eq(" + index + ")").remove();
						main.exchange(g_data[key].ItemList, index, (index + 1));
					}
				});
			}

			propChange(thisPanel, thisEdit, key);
			imgDelete(thisPanel, thisEdit, key);

			//调用主模板JS
			main.saveClick();
			main.moduleActive();
			main.dragEdit();
		}

}
});